package cn.ac.cintcm.prespring.statistics;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * general functions for statistics
 * @author CaoMudan
 * @date 2008-9-13
 */
public class BaseStatistics {
	private String relationTextUrl;
	private String infoTextUrl;
	
	public BaseStatistics(String url1,String url2){
		relationTextUrl=url1;
		infoTextUrl=url2;
	}
	
	/**
	 * count from relationText(format--fjid:zyid1/zyid2/...)
	 * @return map(id,number) id:zyid/zzid/gxid   
	 */
	private Map<String,Integer> countFromText(List<String> fjids){
		if(fjids==null ||fjids.size()==0){
			return null;
		}
		int found=0;
		Map<String,Integer> map=new HashMap<String,Integer>();
		try{
			BufferedReader reader = new BufferedReader(
	                new InputStreamReader(new FileInputStream(relationTextUrl),"UTF8"));
			String line="";
			while((line=reader.readLine())!=null && found<fjids.size()){			
				String[] s=line.split(":");
				if(s.length==2 && fjids.contains(s[0])){
					//System.out.println(line);
					found++;
					String[] entityIds=s[1].split(",");
					for(int i=0;i<entityIds.length;i++){
						String entityId=entityIds[i];
						if(map.get(entityId)==null){
							map.put(entityId,1);
						}else{
							int count=map.get(entityId);
							count++;
							map.put(entityId,count);
						}
					}										
				}
			}
			reader.close();
		}catch(Exception ex){
			System.err.println(ex.getMessage());
		}		
		return map;
	}
	
	/**
	 * get name by id from infoText
	 * @return map(name,number)
	 */
	private Map<String,Integer> getInfoFromText(Map<String,Integer> map){
		Map<String,Integer> enMap = new HashMap<String,Integer>();
		if(map==null ||map.size()==0){
			return enMap;
		}
		int found=0;		
		try{
			BufferedReader reader = new BufferedReader(
	                new InputStreamReader(new FileInputStream(infoTextUrl),"UTF8"));
			String line="";
			Set<String> ids=map.keySet();
			while((line=reader.readLine())!=null && found<map.size()){			
				String[] s=line.split("    ");
				if(s.length==2 && ids.contains(s[0])){
					//System.out.println(line);
					found++;
					enMap.put(s[1],map.get(s[0]));
				}
			}
			reader.close();
		}catch(Exception ex){
			System.err.println(ex.getMessage());
		}		
		return enMap;
	}

	
	/**
	 * 
	 * @param fjids
	 * @return map(name,number)
	 */
	public Map<String,Integer> getStatisticalData(List<String> fjids){
		return getInfoFromText(countFromText(fjids));
	}
	
	public String getInfoTextUrl() {
		return infoTextUrl;
	}

	public void setInfoTextUrl(String infoTextUrl) {
		this.infoTextUrl = infoTextUrl;
	}

	public String getRelationTextUrl() {
		return relationTextUrl;
	}

	public void setRelationTextUrl(String relationTextUrl) {
		this.relationTextUrl = relationTextUrl;
	}
	
}
